
import 'package:flutter/cupertino.dart';
import 'package:sjx/repository/model/home_list_model.dart';
import 'package:sjx/repository/sjx_api.dart';

import '../../repository/model/home_banner_model.dart';

class HomeViewModel with ChangeNotifier {

  List<HomeListItemData>? listData = [];
  int _pageCount = 1;

  Future initDataList(bool loadMore, {ValueChanged<bool>? complete}) async {
    //加载更多
    if (loadMore) {
      _pageCount++;
    } else {
      //重置页码
      _pageCount = 1;
      //刷新数据
      listData?.clear();
    }
    //获取列表
    _getHomeList(loadMore).then((list) {
      listData?.addAll(list ?? []);
      notifyListeners();
      //完成后抛出回调
      complete?.call(loadMore);
    });
  }

  //获取数据
  Future<List<HomeListItemData>?> _getHomeList(bool loadMore) async {
    HomeListModel? data = await SJXApi.instance().homeList(_pageCount);
    if (data != null && data.taskListDetails?.isNotEmpty == true) {
      return data.taskListDetails;
    } else {
      //加载更多场景，拿不到数据，页码-1
      if (loadMore && _pageCount > 0) {
        _pageCount--;
      }
      return [];
    }
  }
}